package com.greateamd.timetableasy.client.mvc.model;

import java.util.ArrayList;
import java.util.List;

import com.greateamd.timetableasy.domain.Event;

public class PlanningModel extends Model {
	
	private List<Event> events = new ArrayList<Event>();
	private List<Event> filteredEvents = new ArrayList<Event>();

	private boolean isFiltered = false;
	private boolean optionalEventUser = true;
	private boolean optionalEventPromotion = true;
    private boolean optionalEventStudyPeriod = true;
    private boolean optionalEventCampus = true;
    private boolean optionalEventUniversity = true;
	
	public PlanningModel(Model model) {
		setUser(model.getUser());
	}

	private void filtreEvent(){
		List<Event> newFilteredEvents = new ArrayList<Event>();
		// While I have events
		for(Event e : events){
			//If event is mandatory we must show this
			if(e.isMandatory())
				newFilteredEvents.add(e);
			else { // Else, i check the filter
				switch(e.getEventType()){
					case USER:
						if(optionalEventUser) newFilteredEvents.add(e); break;
					case PROMOTION:
						if(optionalEventPromotion) newFilteredEvents.add(e); break;
					case STUDY_PERIOD:
						if(optionalEventStudyPeriod) newFilteredEvents.add(e); break;
					case CAMPUS:
						if(optionalEventCampus) newFilteredEvents.add(e); break;
					case UNIVERSITY:
						if(optionalEventUniversity) newFilteredEvents.add(e); break;
					default:
						newFilteredEvents.add(e); break;
				}
			}
		}
		filteredEvents = newFilteredEvents;
		isFiltered = true;
	}

	public List<Event> getFilteredEvents() {
		if(isFiltered)
			return filteredEvents;
		else {
			filtreEvent();
			return filteredEvents;
		}
		
	}

	private List<Event> getEvents() {
		return events;
	}
	
	public void setEvents(List<Event> events) {
		isFiltered = false;
		this.events = events;
	}

	public boolean isOptionalEventUser() {
		return optionalEventUser;
	}

	public boolean isOptionalEventPromotion() {
		return optionalEventPromotion;
	}

	public boolean isOptionalEventStudyPeriod() {
		return optionalEventStudyPeriod;
	}

	public boolean isOptionalEventCampus() {
		return optionalEventCampus;
	}

	public boolean isOptionalEventUniversity() {
		return optionalEventUniversity;
	}

	public void setOptionalEventUser(boolean optionalEventUser) {
		if(!Boolean.valueOf(this.optionalEventUser).equals(optionalEventUser)){
			this.optionalEventUser = optionalEventUser;
			isFiltered = false;
		}
	}

	public void setOptionalEventPromotion(boolean optionalEventPromotion) {
		if(!Boolean.valueOf(this.optionalEventPromotion).equals(optionalEventPromotion)){
			this.optionalEventPromotion = optionalEventPromotion;
			isFiltered = false;
		}
	}

	public void setOptionalEventStudyPeriod(boolean optionalEventStudyPeriod) {
		if(!Boolean.valueOf(this.optionalEventStudyPeriod).equals(optionalEventStudyPeriod)){
			this.optionalEventStudyPeriod = optionalEventStudyPeriod;
			isFiltered = false;
		}
	}

	public void setOptionalEventCampus(boolean optionalEventCampus) {
		if(!Boolean.valueOf(this.optionalEventCampus).equals(optionalEventCampus)){
			this.optionalEventCampus = optionalEventCampus;
			isFiltered = false;
		}
	}

	public void setOptionalEventUniversity(boolean optionalEventUniversity) {
		if(!Boolean.valueOf(this.optionalEventUniversity).equals(optionalEventUniversity)){
			this.optionalEventUniversity = optionalEventUniversity;
			isFiltered = false;
		}
	}

	public boolean isFiltered() {
		return isFiltered;
	}

	public void setFiltered(boolean isFiltered) {
		this.isFiltered = isFiltered;
	}
}
